//
//  MyShipmentDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/10/26.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "MyShipmentDetailVC.h"
#import "MyShipmentNumberCell.h"
#import "MyShipmentAddressCell.h"
#import "MyShipmentInfoCell.h"
#import "MyShipmentGoodCell.h"
#import "MyShipmentOperationCell.h"
#import "WaybillMainModel.h"
#import "RejectOperationView.h"
#import "LJInPutTool.h"
#import "WayBillGuiJiVC.h"
#import "YunDanHuiDanShangJiaoVC.h"
#import "WaybillManagerGoodListVC.h"

@interface MyShipmentDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WaybillMainModel *model;
@property (nonatomic, strong) RejectOperationView *topView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) LJInPutTool *inPutTool;//输入框

@end

@implementation MyShipmentDetailVC

-(LJInPutTool *)inPutTool {
    if (!_inPutTool) {
        _inPutTool = [LJInPutTool new];
        __weak_self;
        _inPutTool.finishBlock = ^(NSString * _Nonnull str) {
            //新建时，驳回
            [weakself boHuiBill:str];
        };
    }
    return _inPutTool;
}

- (void)boHuiBill:(NSString *)str {
    [HttpRequest PostWithApi:ReceiptDeliveryBoHuiMyShipment params:@{@"wa_id":self.wa_id,@"reject":str} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"驳回成功"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshWoFaYunList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {

    }];
}

-(RejectOperationView *)topView {
    if (!_topView) {
        _topView = [RejectOperationView new];
        _topView.canOperation = NO;
        _topView.titleStr = @"运单审核未通过！ ";
    }
    return _topView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
    }
    return _bottomView;
}

//驳回
- (void)rejectClick {
    self.inPutTool.placeholder = @"请输入驳回原因";
    self.inPutTool.sureBtnText = @"确认驳回";
    self.inPutTool.cancleBtnText = @"取消驳回";
    [self.inPutTool showInView:MAIN_WINDOW];
}

//通过
- (void)passClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定通过运单吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self sureBill:self.wa_id];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)sureBill:(NSString *)wa_id {
    [HttpRequest PostWithApi:ReceiptDeliverySureMyShipment params:@{@"wa_id":wa_id} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"确认成功"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshWoFaYunList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {

    }];
}

//查看运输轨迹
- (void)lookCarClick {
    WayBillGuiJiVC *vc = [WayBillGuiJiVC new];
    vc.wa_id = self.wa_id;
    vc.type = @"1";
    [self.rt_navigationController pushViewController:vc animated:YES];
}

//回单上缴
- (void)huiDanShangJiao {
    YunDanHuiDanShangJiaoVC *vc = [YunDanHuiDanShangJiaoVC new];
    vc.wa_id = self.wa_id;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(WaybillMainModel *)model {
    if (!_model) {
        _model = [WaybillMainModel new];
    }
    return _model;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topView.nim_bottom, ScreenW, ScreenH - NaviHeight - self.topView.nim_height - self.bottomView.nim_height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryMyShipmentDetail params:@{@"wa_id":self.wa_id,@"type":@"1"} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [WaybillMainModel mj_objectWithKeyValues:responseObject];
            [self configUI];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)configUI {
    if ([self.model.status isEqualToString:@"1"]) {
        //受理
        [self.view addSubview:self.bottomView];
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *rejectBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [rejectBtn setBackgroundColor:HexColor(0xF35959)];
        rejectBtn.layer.masksToBounds = YES;
        rejectBtn.layer.cornerRadius = 6;
        [rejectBtn setImage:[UIImage imageNamed:@"icon_reject"] forState:0];
        [rejectBtn setTitle:@"驳回" forState:0];
        [rejectBtn setTitleColor:HexColor(0xffffff) forState:0];
        rejectBtn.titleLabel.font = BoldFontOfSize(16);
        [rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
        rejectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:rejectBtn];
        
        UIButton *passBtn = [[UIButton alloc] initWithFrame:CGRectMake(rejectBtn.nim_right + 15, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [passBtn setBackgroundColor:Color_ThemeColor];
        passBtn.layer.masksToBounds = YES;
        passBtn.layer.cornerRadius = 6;
        [passBtn setImage:[UIImage imageNamed:@"icon_adopt"] forState:0];
        [passBtn setTitle:@"通过" forState:0];
        [passBtn setTitleColor:HexColor(0xffffff) forState:0];
        passBtn.titleLabel.font = BoldFontOfSize(16);
        [passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
        passBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:passBtn];
        
        [self.topView removeFromSuperview];
        self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
    }else if ([self.model.status isEqualToString:@"3"]) {
        //驳回
        [self.bottomView removeFromSuperview];
        [self.view addSubview:self.topView];
        self.topView.canOperation = NO;
        self.topView.frame = CGRectMake(0, 0, ScreenW, _topView.height);
        self.topView.titleStr = @"运单审核未通过！ ";
        self.topView.timeStr = self.model.rejectTime;
        self.topView.descStr = self.model.reject_remarks;
        self.tableView.frame = CGRectMake(0, self.topView.nim_bottom, ScreenW, ScreenH - NaviHeight - BottomHeight - self.topView.nim_height);
    }else {
        if ([self.model.status isEqualToString:@"12"]) {
            //签收状态下
            if ([self.model.isTurn isEqualToString:@"1"]) {
                //需要回单上缴
                [self.view addSubview:self.bottomView];
                [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                UIButton *rejectBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
                [rejectBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
                rejectBtn.layer.masksToBounds = YES;
                rejectBtn.layer.cornerRadius = 6;
                [rejectBtn setImage:[UIImage imageNamed:@"wofayun_icon_location"] forState:0];
                [rejectBtn setTitle:@"查看运输轨迹" forState:0];
                [rejectBtn setTitleColor:Color_ThemeColor forState:0];
                rejectBtn.titleLabel.font = BoldFontOfSize(16);
                [rejectBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
                rejectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
                [self.bottomView addSubview:rejectBtn];
                
                UIButton *passBtn = [[UIButton alloc] initWithFrame:CGRectMake(rejectBtn.nim_right + 15, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
                [passBtn setBackgroundColor:Color_ThemeColor];
                passBtn.layer.masksToBounds = YES;
                passBtn.layer.cornerRadius = 6;
                [passBtn setTitle:@"回单上缴" forState:0];
                [passBtn setTitleColor:HexColor(0xffffff) forState:0];
                passBtn.titleLabel.font = BoldFontOfSize(16);
                [passBtn addTarget:self action:@selector(huiDanShangJiao) forControlEvents:UIControlEventTouchUpInside];
                passBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
                [self.bottomView addSubview:passBtn];
                
                [self.topView removeFromSuperview];
                self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
            }else {
                //其他情况，显示查看路线按钮
                [self.topView removeFromSuperview];
                [self.view addSubview:self.bottomView];
                [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
                [nextBtn setBackgroundColor:Color_ThemeColor];
                nextBtn.layer.masksToBounds = YES;
                nextBtn.layer.cornerRadius = 6;
                [nextBtn setImage:[UIImage imageNamed:@"icon_location"] forState:0];
                [nextBtn setTitle:@"查看运输轨迹" forState:0];
                [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
                nextBtn.titleLabel.font = BoldFontOfSize(16);
                [nextBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
                [self.bottomView addSubview:nextBtn];
                self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
            }
        }else {
            //其他情况，显示查看路线按钮
            [self.topView removeFromSuperview];
            [self.view addSubview:self.bottomView];
            [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
            [nextBtn setBackgroundColor:Color_ThemeColor];
            nextBtn.layer.masksToBounds = YES;
            nextBtn.layer.cornerRadius = 6;
            [nextBtn setImage:[UIImage imageNamed:@"icon_location"] forState:0];
            [nextBtn setTitle:@"查看运输轨迹" forState:0];
            [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
            nextBtn.titleLabel.font = BoldFontOfSize(16);
            [nextBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
            [self.bottomView addSubview:nextBtn];
            self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
        }
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.model.status isEqualToString:@"14"]) {
        //完成
        return 5;
    }else {
        return 4;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyShipmentNumberCell *cell = [MyShipmentNumberCell MyShipmentNumberCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else if (indexPath.row == 1) {
        MyShipmentAddressCell *cell = [MyShipmentAddressCell MyShipmentAddressCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else if (indexPath.row == 2) {
        if ([self.model.status isEqualToString:@"15"]) {
            MyShipmentOperationCell *cell = [MyShipmentOperationCell MyShipmentOperationCellWithTableView:tableView];
            cell.model = self.model;
            return cell;
        }else {
            MyShipmentInfoCell *cell = [MyShipmentInfoCell MyShipmentInfoCellWithTableView:tableView];
            cell.model = self.model;
            return cell;
        }
    }else if (indexPath.row == 3) {
        if ([self.model.status isEqualToString:@"15"]) {
            MyShipmentInfoCell *cell = [MyShipmentInfoCell MyShipmentInfoCellWithTableView:tableView];
            cell.model = self.model;
            return cell;
        }else {
            MyShipmentGoodCell *cell = [MyShipmentGoodCell MyShipmentGoodCellWithTableView:tableView];
            cell.model = self.model;
            __weak_self;
            cell.listBlock = ^{
                WaybillManagerGoodListVC *vc = [WaybillManagerGoodListVC new];
                vc.wa_id = weakself.model.wa_id;
                vc.type = @"1";
                [weakself.rt_navigationController pushViewController:vc animated:YES];
            };
            return cell;
        }
    }else if (indexPath.row == 4) {
        MyShipmentGoodCell *cell = [MyShipmentGoodCell MyShipmentGoodCellWithTableView:tableView];
        cell.model = self.model;
        __weak_self;
        cell.listBlock = ^{
            WaybillManagerGoodListVC *vc = [WaybillManagerGoodListVC new];
            vc.wa_id = weakself.model.wa_id;
            vc.type = @"1";
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }
    UITableViewCell *cell = [UITableViewCell new];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyShipmentNumberCell *cell = [MyShipmentNumberCell MyShipmentNumberCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else if (indexPath.row == 1) {
        MyShipmentAddressCell *cell = [MyShipmentAddressCell MyShipmentAddressCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else if (indexPath.row == 2) {
        if ([self.model.status isEqualToString:@"15"]) {
            MyShipmentOperationCell *cell = [MyShipmentOperationCell MyShipmentOperationCellWithTableView:tableView];
            cell.model = self.model;
            return cell.height;
        }else {
            MyShipmentInfoCell *cell = [MyShipmentInfoCell MyShipmentInfoCellWithTableView:tableView];
            cell.model = self.model;
            return cell.height;
        }
    }else if (indexPath.row == 3) {
        if ([self.model.status isEqualToString:@"15"]) {
            MyShipmentInfoCell *cell = [MyShipmentInfoCell MyShipmentInfoCellWithTableView:tableView];
            cell.model = self.model;
            return cell.height;
        }else {
            MyShipmentGoodCell *cell = [MyShipmentGoodCell MyShipmentGoodCellWithTableView:tableView];
            cell.model = self.model;
            return cell.height;
        }
    }else if (indexPath.row == 4) {
        MyShipmentGoodCell *cell = [MyShipmentGoodCell MyShipmentGoodCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
